import {
	getProdStatus
} from '@/module/product/prodMet';
import {
	countTime
} from '@/utils/moment/index.js';
export const auctionMixin = {
	data() {
		return {
			dataForm: {}, // 基本信息
			prodStockForm: {
				start_date: '',
				end_date: '',
				intend: 10, // 准备时间
				duration: 40, // 每轮时间
				auctionTime: 30, // 抢拍时间
				interval: 20, // 场次间隔
				price_breaks: 10, // 每秒降价
				amount: 0
			}, // 库存
			auction_num: [], // 场次列表
			prodState: {
				type: null, // 0未开始，1进行中，2已结束,3
				times: 0 // 初始化查询是，距离下个状态的时间戳差值
			}, //产品状态
			auctionType: 1, // 1:准备阶段 2:抢购阶段
			timer: null, // 倒计时定时器
			timer2: null,
			timeObj: {}, // 活动倒计时时间对象
			currentPrice: 0, // 当前显示价格
			currentIndex: -1, // 当前轮次
			currentStock: 1, // 当前剩余库存
			init:false
		}
	},
	methods: {
		initHandle() {
			// 加载数据
			this.prodState = getProdStatus(this.prodStockForm.start_date, this.prodStockForm.end_date);
			if (this.prodState.type === 0) {
				// 未开始
				this.currentIndex = 0;
				this.getCountTime(this.prodState.times);
			} else if (this.prodState.type === 1) {
				// 活动进行中
				this.whichOne();
			}
			this.init = true
		},
		// 定时器循环倒计时
		getCountTime(nextTime) {
			if (nextTime > 0) {
				this.timeObj = countTime(nextTime);
				this.timer = setTimeout(() => {
					this.getCountTime(this.timeObj.nextTime);
				}, 1000);
			} else {
				// 开始拍卖
				this.whichOne();
			}
		},
		// 判断有没有场次
		whichOne() {
			if (this.getStockTime) {
				clearTimeout(this.getStockTime)
			}
			this.currentStock = 1
			let date = new Date();
			let current = date.getTime(); // 当前时间
			let bol = 0;
			if (this.currentIndex > -1) {
				this.auction_num.splice(0, this.currentIndex);
			}
			this.currentIndex = this.auction_num.findIndex(item => {
				if (current >= item.start_date && current < item.end_date) {
					// 正在进行某一轮(准备/抢购)
					bol = 2;
					return true;
				} else if (current < item.start_date) {
					bol = 1;
					// 待开始下一轮
					return true;
				}
				return false;
			});
			if (bol == 2) {
				this.prodState.type = 1
				// (准备/抢购)
				let cycelSurplus = Math.round((this.auction_num[this.currentIndex].end_date - current) /
					1000); // 当前阶段剩余秒
				let auctionShowTime = cycelSurplus - this.prodStockForm.auctionTime;
				if (auctionShowTime > 0) {
					// 还在准备时间阶段
					this.auctionType = 1;
					this.currentPrice = this.prodStockForm.amount;
					this.timeObj = countTime(auctionShowTime, false)
					this.countDown(this.timeObj.nextTime, true);
				} else {
					// 抢拍阶段
					this.auctionType = 2;
					let price = this.prodStockForm.amount - (this.prodStockForm.auctionTime -
						cycelSurplus) * this.prodStockForm.price_breaks // 剩余价格
					if(price > this.prodStockForm.platfor_price){
						this.currentPrice = price
					}else{
						this.currentStock = 0
						this.currentPrice = this.prodStockForm.platfor_price
					}
					this.timeObj = countTime(cycelSurplus, false)
					this.countDown(this.timeObj.nextTime);
					if (this.toGetStock) {
						this.toGetStock()
					}
				}
			} else if (bol == 1) {
				this.currentPrice = this.prodStockForm.amount;
				// 未开始
				let date = new Date();
				let current = date.getTime(); // 当前时间
				this.prodState = {
					msg: '距开始',
					times: this.auction_num[this.currentIndex].start_date - current, // 距离开始所需时间
					type: 0
				};
				this.getCountTime(this.prodState.times);
			} else {
				this.prodState = {
					type: 2,
					times: 0,
					msg: '活动已结束'
				};
			}
		},
		countDown(nextTime, isReady, bol) {
			if (nextTime >= 0) {
				this.timer2 = setTimeout(() => {
					this.timeObj = countTime(nextTime, false);
					this.countDown(this.timeObj.nextTime, isReady);
					if (!isReady) {
						// 计算价格
						let price = this.currentPrice - this.prodStockForm.price_breaks
						if(price > this.prodStockForm.platfor_price){
							this.currentPrice = price
						}else{
							this.currentStock = 0
							this.currentPrice = this.prodStockForm.platfor_price
						}
					}
				}, 1000);
			} else if (isReady) {
				// 从准备阶段转到抢购下单阶段
				setTimeout(() => {
					this.auctionType = 2;
				}, 1000)
				// this.currentPrice = this.currentPrice
				// this.timeObj = countTime(this.prodStockForm.auctionTime, false);
				this.countDown(this.prodStockForm.auctionTime - 1);
				if (this.toGetStock) {
					this.toGetStock()
				}
			} else {
				if (this.currentIndex < this.auction_num.length - 1) {
					this.timer2 = setTimeout(() => {
						// 当前轮已结束，开始下一轮
						this.whichOne();
					}, 1000);
				} else {
					// 没有下一轮了,重新刷新页面数据，看一下后面还有没有数据
					if (this.isDetail) {
						this.initPage(this.dataForm.pk_id);
					}
					if (this.getStockTime) {
						clearTimeout(this.getStockTime)
					}
					this.prodState = {
						type: 2,
						times: 0,
						msg: '活动已结束'
					};
				}
			}
		},
	},
}
